package cn.iocoder.yudao.module.shop.service.giftitem;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.giftitem.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftitem.ShopGiftItemDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.giftitem.ShopGiftItemConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.giftitem.ShopGiftItemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 礼品项 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopGiftItemServiceImpl implements ShopGiftItemService {

    @Resource
    private ShopGiftItemMapper giftItemMapper;

    @Override
    public Long createGiftItem(ShopGiftItemCreateReqVO createReqVO) {
        // 插入
        ShopGiftItemDO giftItem = ShopGiftItemConvert.INSTANCE.convert(createReqVO);
        giftItemMapper.insert(giftItem);
        // 返回
        return giftItem.getId();
    }

    @Override
    public void updateGiftItem(ShopGiftItemUpdateReqVO updateReqVO) {
        // 校验存在
        validateGiftItemExists(updateReqVO.getId());
        // 更新
        ShopGiftItemDO updateObj = ShopGiftItemConvert.INSTANCE.convert(updateReqVO);
        giftItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteGiftItem(Long id) {
        // 校验存在
        validateGiftItemExists(id);
        // 删除
        giftItemMapper.deleteById(id);
    }

    private void validateGiftItemExists(Long id) {
        if (giftItemMapper.selectById(id) == null) {
            throw exception(GIFT_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public ShopGiftItemDO getGiftItem(Long id) {
        return giftItemMapper.selectById(id);
    }

    @Override
    public List<ShopGiftItemDO> getGiftItemList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return giftItemMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopGiftItemDO> getGiftItemPage(ShopGiftItemPageReqVO pageReqVO) {
        return giftItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopGiftItemDO> getGiftItemList(ShopGiftItemExportReqVO exportReqVO) {
        return giftItemMapper.selectList(exportReqVO);
    }

}
